/*
*********************************************************************************************************
*
*                                           flash.h
*
* 文件名称 : flash.h
* 文件版本 : V0.01
* 文件描述 : 内部flash读写
* 编写人员 : HHL
* 修改人员 :
* 编修描述 : V0.01，时间2013.11.30
*********************************************************************************************************
*/
#ifndef __flash_h
#define __flash_h
#include <stdint.h>
#include "commData.h"
#include "stm32f4xx_flash.h"

#define  _addrs_16_kb      0x03fff
#define  _addrs_64_kb      0x0ffff
#define  _addrs_128_kb     0x1ffff
#define  _addrs_30_kb      0x077ff

#define STM32F407_FLASH_ADDR_BASE                  (0x08000000)
//SECTOR_0
#define STM32F407_FLASH_ADDT_SECTOR_0_HEAD         (STM32F407_FLASH_ADDR_BASE)
#define STM32F407_FLASH_ADDT_SECTOR_0_END          (STM32F407_FLASH_ADDT_SECTOR_0_HEAD + _addrs_16_kb)
//SECTOR_1
#define STM32F407_FLASH_ADDT_SECTOR_1_HEAD         (STM32F407_FLASH_ADDT_SECTOR_0_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_1_END          (STM32F407_FLASH_ADDT_SECTOR_1_HEAD + _addrs_16_kb)
//SECTOR_2
#define STM32F407_FLASH_ADDT_SECTOR_2_HEAD         (STM32F407_FLASH_ADDT_SECTOR_1_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_2_END          (STM32F407_FLASH_ADDT_SECTOR_2_HEAD + _addrs_16_kb)
//SECTOR_3
#define STM32F407_FLASH_ADDT_SECTOR_3_HEAD         (STM32F407_FLASH_ADDT_SECTOR_2_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_3_END          (STM32F407_FLASH_ADDT_SECTOR_3_HEAD + _addrs_16_kb)
//SECTOR_4
#define STM32F407_FLASH_ADDT_SECTOR_4_HEAD         (STM32F407_FLASH_ADDT_SECTOR_3_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_4_END          (STM32F407_FLASH_ADDT_SECTOR_4_HEAD + _addrs_64_kb)
//SECTOR_5
#define STM32F407_FLASH_ADDT_SECTOR_5_HEAD         (STM32F407_FLASH_ADDT_SECTOR_4_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_5_END          (STM32F407_FLASH_ADDT_SECTOR_5_HEAD + _addrs_128_kb)
//SECTOR_6
#define STM32F407_FLASH_ADDT_SECTOR_6_HEAD         (STM32F407_FLASH_ADDT_SECTOR_5_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_6_END          (STM32F407_FLASH_ADDT_SECTOR_6_HEAD + _addrs_128_kb)
//SECTOR_7
#define STM32F407_FLASH_ADDT_SECTOR_7_HEAD         (STM32F407_FLASH_ADDT_SECTOR_6_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_7_END          (STM32F407_FLASH_ADDT_SECTOR_7_HEAD + _addrs_128_kb)
//SECTOR_8
#define STM32F407_FLASH_ADDT_SECTOR_8_HEAD         (STM32F407_FLASH_ADDT_SECTOR_7_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_8_END          (STM32F407_FLASH_ADDT_SECTOR_8_HEAD + _addrs_128_kb)
//SECTOR_9
#define STM32F407_FLASH_ADDT_SECTOR_9_HEAD         (STM32F407_FLASH_ADDT_SECTOR_8_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_9_END          (STM32F407_FLASH_ADDT_SECTOR_9_HEAD + _addrs_128_kb)
//SECTOR_10
#define STM32F407_FLASH_ADDT_SECTOR_10_HEAD        (STM32F407_FLASH_ADDT_SECTOR_9_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_10_END         (STM32F407_FLASH_ADDT_SECTOR_10_HEAD + _addrs_128_kb)
//SECTOR_11
#define STM32F407_FLASH_ADDT_SECTOR_11_HEAD        (STM32F407_FLASH_ADDT_SECTOR_10_END  + 1)
#define STM32F407_FLASH_ADDT_SECTOR_11_END         (STM32F407_FLASH_ADDT_SECTOR_11_HEAD + _addrs_128_kb)
//SYSTEM
#define STM32F407_FLASH_ADDT_SYSTEM_HEAD           (0x1fff0000)
#define STM32F407_FLASH_ADDT_SYSTEM_END            (STM32F407_FLASH_ADDT_SYSTEM_HEAD + _addrs_30_kb)

#define  SYS_SET_FLASH_HEAD_ADDR                   (STM32F407_FLASH_ADDT_SECTOR_11_HEAD + 8)
#define  SYS_SET_FLASH_HEAD_FLAG                   (0xaaaa0000)
#define  SYS_SET_FLASH_END_FLAG					   (0x0000aaaa)

//重置到工厂模式选项
#define FACTORY_RESET_RTC       (1)
#define FACTORY_RESET_FLASH     (1<<1)



/*
*********************************************************************************************************
*                                           函数申明
*********************************************************************************************************
*/

//写入word
uint32_t writeFlashWord(uint32_t flashAddr, uint32_t *pdata, uint32_t len);
//读取word
uint32_t readFlashWord(uint32_t flashAddr, uint32_t *pdata, uint32_t len);
//hhhhH
uint32_t eraseFlashSector(uint32_t flashAddr);
//重新设置
void resetFactory(uint32_t set);
#endif
